package com.axis2.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.Socket;
import java.util.Date;

/**
 * 
* @ClassName: SocketHelper    
* @Description: 辅助器 单例  
* @author xxliu   
* @date 2016年7月21日 下午3:40:35    
* @版权所有 环迅支付
 */
public class SocketHelper {
	
	/**
	 * 日志对象
	 */
	private static Logger logger = LoggerFactory.getLogger(SocketHelper.class);
	
	private static SocketHelper socketHelper = null;
	
	private SocketHelper() {}
	
	public static SocketHelper getInstance() {// 实例化引用
	        if (socketHelper == null) {
	        	socketHelper = new SocketHelper();
	        }
	        return socketHelper;
	}
	/**
	 * socket关键字
	 */
	private static String socketKey;

	/**
	 * socket客户端对象
	 */
	private static Socket socket;
	
	private static  Thread checkThread;
	private static  Thread sendThread;
	private static  Thread receiveThread;

	/**
	 * 允许运行标识
	 */
	private static volatile boolean canRun = true;

	/**
	 * 最后活动时间
	 */
	private static Date lastActiveTime = new Date();

	/**
	 * 已经接收的粘包块
	 */
	private static byte[] receivedBytes;

	/**
	 * @return the socketKey
	 */
	public String getSocketKey() {
		return socketKey;
	}

	/**
	 * @param socketKey
	 *            the socketKey to set
	 */
	public void setSocketKey(String socketKey) {
		this.socketKey = socketKey;
	}

	/**
	 * @return the socket
	 */
	public Socket getSocket() {
		return socket;
	}

	/**
	 * @param socket
	 *            the socket to set
	 */
	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	/**
	 * @return the lastActiveTime
	 */
	public Date getLastActiveTime() {
		return lastActiveTime;
	}

	/**
	 * @param lastActiveTime
	 *            the lastActiveTime to set
	 */
	public void setLastActiveTime(Date lastActiveTime) {
		this.lastActiveTime = lastActiveTime;
	}

	/**
	 * @return the receivedBytes
	 */
	public byte[] getReceivedBytes() {
		return receivedBytes;
	}

	/**
	 * @param receivedBytes
	 *            the receivedBytes to set
	 */
	public void setReceivedBytes(byte[] receivedBytes) {
		this.receivedBytes = receivedBytes;
	}
	
	@Override
	protected void finalize() throws Throwable{
		logger.info("finalize 垃圾回收…………");
		if(socket != null){
			socket.close();
		}
	}

	/**
	 * @return checkThread
	 */
	public static Thread getCheckThread() {
		return checkThread;
	}

	/**
	 * Thread
	 */
	public static void setCheckThread(Thread checkThread) {
		SocketHelper.checkThread = checkThread;
	}

	/**
	 * @return sendThread
	 */
	public static Thread getSendThread() {
		return sendThread;
	}

	/**
	 * Thread
	 */
	public static void setSendThread(Thread sendThread) {
		SocketHelper.sendThread = sendThread;
	}

	/**
	 * @return receiveThread
	 */
	public static Thread getReceiveThread() {
		return receiveThread;
	}

	/**
	 * Thread
	 */
	public static void setReceiveThread(Thread receiveThread) {
		SocketHelper.receiveThread = receiveThread;
	}

	/**
	 * @return canRun
	 */
	public boolean isCanRun() {
		return canRun;
	}

	/**
	 * boolean
	 */
	public void setCanRun(boolean canRun) {
		this.canRun = canRun;
	}
	
	
}
